###############################################################################-
# Author: Pietryka
# Contact: matthew.pietryka@gmail.com
# Purpose: create Table 4: Placebo Tests 
# Notes:
###############################################################################-

# Load Packages  =====================

library(dplyr)
library(tidyr)
library(readr)
library(purrr)
library(flextable)
library(officer)

# Load Data ==================================

# object from "Permutation Tests 2 - Summarize Outcomes.R"
estimates_df <- read_rds("data-files/estimates_df.rds")
estimates_gender_df <- read_rds("data-files/estimates_gender_df.rds")

source("Plot Preferences.R")


# Function to create tables ======================

get_flextable_defaults()


set_flextable_defaults(
  font.family = the_typeface,
  font.color = "black",
  font.size = 9
)

big_border = fp_border(color="grey50", width = 1)

std_border = fp_border(color="grey75", width = 0.5)



format_table <- function(df){
  df  %>% 
    mutate(across(where(is.double), \(x) round(x, digits = 2))) %>% 
    flextable()  %>% 
    align_nottext_col(align = "center") %>% 
    align_text_col(align = "right") %>% 
    border_remove() %>% 
    border_inner_h(border = std_border, part = "body" ) %>% 
    border_inner_h(border = big_border, part = "header" ) %>% 
    autofit(part = "body") %>% 
    bold(part = "header") %>% 
    add_header(
      sim_estimate_unconstrained_mean = "Permutation Mean", 
      sim_estimate_mean = "Permutation Mean",
      p_value_unconstrained = "p-value",
      p_value = "p-value"
    )  %>% 
    merge_h(part = "header") %>% 
    set_header_labels(
      lab = "Variable",
      observed_estimate = "Observed Estimate",
      sim_estimate_unconstrained_mean = "Simple",
      sim_estimate_mean = "Constrained",
      p_value_unconstrained = "Simple",
      p_value = "Constrained"
    )  %>% 
    style(i=1,part = "header", pr_t = fp_text_default(underlined = TRUE, bold = TRUE))  %>% 
    hline(part = "header", i = 2)
}


# placebo Plots ==================================





placebo_stats_df <- tribble(
  ~order, ~name, ~lab, 
   1, "prop_identical_parents_2012" , "Proportion whose parents had identical turnout in 2012",
   2, "prop_identical_parents_2014" , "Proportion whose parents had identical turnout in 2014",
   3, "cor_parents_turnout_mean0" , "Correlation in mean parents' turnout, 2008-2014"
)


placebo_tab <- estimates_df  %>% 
  inner_join(placebo_stats_df)  %>% 
  arrange(order) %>% 
  select(
    lab,
    observed_estimate,
    sim_estimate_unconstrained_mean,
    sim_estimate_mean,
    p_value_unconstrained,
    p_value
  )  %>% 
  format_table() 
  
  
  

placebo_tab


# by gender --------------------

placebo_gender_tab <- estimates_gender_df  %>%
  inner_join(placebo_stats_df)  %>% 
  arrange(order)  %>% 
  mutate(gender = ifelse(gender_f == "female", "Women", "Men"))  %>% 
  arrange(order, desc(gender))  %>% 
  select(
    lab,
    Gender = gender,
    observed_estimate,
    sim_estimate_unconstrained_mean,
    sim_estimate_mean,
    p_value_unconstrained,
    p_value
  ) %>% 
  format_table() %>% 
  align(j = 2, align = "center")

placebo_gender_tab

# save tables ======================================================

save_as_docx(
  placebo_tab, 
  path = "Results/placebo-table.docx"
  )


save_as_docx(
  placebo_gender_tab, 
  path = "Results/placebo-gender-table.docx"
)




